package com.common.util;
import java.lang.reflect.*;




public class BeanUtils {

	private BeanUtils() {
	}
	
	/**暴力获得某属性值
	 * @param object
	 * @param propertyName
	 * @return
	 */
	public static Object forceGetProperty(Object object, String propertyName)  {
	Object result = null;
	Class classType = object.getClass();
	String fristLetter = propertyName.substring(0, 1).toUpperCase();
	String getMethodName = "get"+fristLetter+propertyName.substring(1);
	try {
		Method getMethod = classType.getMethod(getMethodName, new Class[]{});
		result = getMethod.invoke(object, new Object[]{});
	} catch (Exception e) {
		e.printStackTrace();
	} 
		
		return result;
	}
	
	
	/**暴力设置某属性值
	 * @param object
	 * @param propertyName
	 * @param newValue
	 * @throws NoSuchFieldException
	 */
	public static void forceSetProperty(Object object, String propertyName, Object newValue)
			throws NoSuchFieldException {
		Class classType = object.getClass();
		String fristLetter = propertyName.substring(0, 1).toUpperCase();
		try {
			Method setMethod = classType.getMethod("set"+fristLetter+propertyName.substring(1), new Class[]{newValue.getClass()});
			setMethod.invoke(object, new Object[]{newValue});
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
	}

	/**
	 * <li>@方法说明:调用类静态方法
	 * <li>@param obj 类名“com.cddts.uphold.report.service.ReportSQLService”
	 * <li>@param methodName 参数名“findErrorList”
	 * <li>@param param 参数值
	 * <li>@return
	 * <li>@创建人:王剑
	 * <li>@创建日期:2010-4-27
	 * <li>@修改人: 
	 * <li>@修改日期:
	 * <li>@详细说明: 
	 */
	public static Object doStaticMethod(String obj,String methodName,Object... param){
		Object reObject = null;
		Class[] par =new Class[param.length];
		for(int i=0;i<param.length;i++){
			par[i]=param[i].getClass();
		}
		try {
			Class clazz = Class.forName(obj);
			Method method = clazz.getMethod(methodName,par);
			reObject = method.invoke(clazz, param);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return reObject;
	}
	
	/**
	 * 
	 * <li>方法说明：调用对象方法
	 * <li>@param object 对象名
	 * <li>@param methodName 方法名
	 * <li>@param params 参数对象
	 * <li>@return
	 * <li>@throws NoSuchMethodException 如果没有该Method时抛出.
	 * <li>返回类型：Object
	 * <li>说明：暴力调用对象方法,忽略private,protected修饰符的限制.
	 * <li>创建人：王剑
	 * <li>创建日期：2008-10-14
	 * <li>修改人： 
	 * <li>修改日期：
	 */
	public static Object invokePrivateMethod(Object object, String methodName, Object... params)throws NoSuchMethodException {

		Class[] types = new Class[params.length];
		for (int i = 0; i < params.length; i++) {
			types[i] = params[i].getClass();
		}
		
		Class clazz = object.getClass();
		Method method = null;
		for (Class superClass = clazz; superClass != Object.class; superClass = superClass.getSuperclass()) {
			try {
				method = superClass.getDeclaredMethod(methodName, types);
				break;
			} catch (NoSuchMethodException e) {
				// 方法不在当前类定义,继续向上转型
			}
		}
		
		if (method == null)
			throw new NoSuchMethodException("No Such Method:" + clazz.getSimpleName() + methodName);
		
		boolean accessible = method.isAccessible();
		method.setAccessible(true);
		Object result = null;
		try {
			result = method.invoke(object, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		method.setAccessible(accessible);
		return result;
	}
	
	public static StringBuffer toStr(Object object) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		  Class clazz = object.getClass();  
	        Field[] fields = clazz.getDeclaredFields();  
	        StringBuffer sb = new StringBuffer("");  
	        for (Field field : fields) {  
	            String fieldName = field.getName();  
	            String fieldType = field.getGenericType().toString();
	            Method method = clazz.getMethod("get" + change(fieldName), null);  
	            Object property = method.invoke(object, null);  
	            sb.append(fieldName+"=" + property + ",");  
	             
	        }                  
	        return sb; 
	}
	
	public static String change(String src) {  
      if (src != null) {  
          StringBuffer sb = new StringBuffer(src);  
          sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));  
          return sb.toString();  
      } else {  
          return null;  
      }  
  }  
}